package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Message;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class MessageDao extends BaseDao<Message> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<Message> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Message> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Message> list = run.query(sql, new ResultSetHandler<List<Message>>() {
            @Override
            public List<Message> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Message message = new Message();

                    message.setId(rs.getInt("id"));
                    message.setCreated(rs.getTimestamp("created"));
                    message.setEvent(rs.getInt("event"));
                    message.setFromId(rs.getInt("from_id"));
                    message.setPostId(rs.getInt("post_id"));
                    message.setStatus(rs.getInt("status"));
                    message.setUserId(rs.getInt("user_id"));

                    list.add(message);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  Message 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Message selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, created, event, from_id, post_id, status, user_id from mto_message where id = ?";
        Message message = run.query(sql,new ResultSetHandler<Message>() {
            @Override
            public Message handle(ResultSet rs) throws SQLException {
                Message message = null;
                if(rs.next()) {
                    message = new Message();

                        message.setId(rs.getInt("id"));
                        message.setCreated(rs.getTimestamp("created"));
                        message.setEvent(rs.getInt("event"));
                        message.setFromId(rs.getInt("from_id"));
                        message.setPostId(rs.getInt("post_id"));
                        message.setStatus(rs.getInt("status"));
                        message.setUserId(rs.getInt("user_id"));
                }
                return  message;
            }
        },params);
        return message;
    }

    /**
    *
    * @param message 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Message message) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_message (  ");
        sql.append("  created, event, from_id, post_id, status, user_id ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            message.getCreated(),  
            message.getEvent(),  
            message.getFromId(),  
            message.getPostId(),  
            message.getStatus(),  
            message.getUserId()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  message
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Message message) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_message           ");
        sql.append(" SET                    ");
            
            sql.append("   created  = ?,  ");
            sql.append("   event  = ?,  ");
            sql.append("   from_id  = ?,  ");
            sql.append("   post_id  = ?,  ");
            sql.append("   status  = ?,  ");
            sql.append("   user_id  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                message.getCreated(), 
                message.getEvent(), 
                message.getFromId(), 
                message.getPostId(), 
                message.getStatus(), 
                message.getUserId(), 
            message.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_message where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
